;(function(){

    // 负责拿数据，渲染界面
    class Rendering{
        constructor(){
            this.userData = JSON.parse(sessionStorage.getItem("data"))[0];
            this.noGoodsDataEle = $("#cartNoGoods");
            this.haveGoodsDataEle = $("#cartHaveGoods");
            this.allCartGoods = $(`.content_bigbox`);
        }
        getData(){
            var that = this;
            pAjax({
                url:"http://localhost/XMYP/src/php/cart.php",
                type:"get",
                data:{
                    user:that.userData.user,
                }
            }).then((res)=>{
                this.goodsData = JSON.parse(res);
                if(this.goodsData.code==1){
                    this.closeEle();
                }else if(!this.goodsData.code){
                    this.addEle();
                }
                dataStorage.getData(this.goodsData);
            });
        }
        addEle(){
            this.noGoodsDataEle.css("display","none");
            this.haveGoodsDataEle.css("display","block");
           
            var str =`
                <div class="content_goods_shop">
                    <i class="selectGoods"></i>
                    <span>有道精选</span>
                    <span>
                        <span>!</span>
                        满99.00元免运费
                    </span>
                </div>
                <div class="content_goods_discount">
                    <span>满减</span>
                    <span>米粉节满199减40；299减60；399减80</span>
                    <span>凑单></span>
                </div>
            `;
            for(var i = 0 ; i < this.goodsData.length; i++){
                str += `
                    <div class="content_goods_box">
                        <div class="content_goodsbox_select">
                            <i class="selectGoods" state="0"></i>
                        </div>
                        <div class="content_goodsbox_img">
                            <img src="${this.goodsData[i].img}" alt="">
                        </div>
                        <div class="content_goodsbox_title">
                            <p>${this.goodsData[i].goodsMsg}</p>
                        </div>
                        <div class="content_goodsbox_price">
                            <span>￥${this.goodsData[i].price}</span>
                        </div>
                        <div class="content_goodsbox_num">
                            <div class="content_goodsbox_num_box">
                                <div class="reduceBtn">-</div>
                                <div>${this.goodsData[i].num}</div>
                                <div class="addBtn">+</div>
                            </div>
                        </div>
                        <div class="content_goodsbox_allprice">
                            <span>￥${this.goodsData[i].price*this.goodsData[i].num}</span>
                        </div>
                        <div class="content_goodsbox_del">
                            <i>x</i>
                        </div>
                    </div>
                `;
            }
            this.allCartGoods.html(str);
            // 读取页面的选择按钮
            calculation.addEvent();
            // 渲染之后才拿到加减按钮
            dataStorage.addEvent();

        }
        closeEle(){
            this.noGoodsDataEle.css("display","block");
            this.haveGoodsDataEle.css("display","none");
        }
        addEvent(){}
        dump(){}
    }


    let rendering = new Rendering();
    rendering.getData();

    // 负责处理选择商品的按钮和计算价格数量
    class Calculation{
        constructor(){
        }
        addEvent(){
            var that =this;
            this.secletgoods = $(".selectGoods");
            // 判断是否选中状态
            this.state = 0 ;
            // 循环添加事件
            for(let i = 0 ; i < that.secletgoods.length; i++){
                // 判断是不是全选
               if(i==0 || i==1 || i==this.secletgoods.length-1){
                    this.secletgoods.eq(i).on("click",function(){
                        if(that.state == 0 ){
                            for(let k = 0 ; k < that.secletgoods.length; k++){
                                that.secletgoods.eq(k).addClass("active");
                                that.secletgoods.eq(k).attr("state",1);
                            }
                            that.state = 1 ;
                        }else if(that.state == 1 ){
                            for(let k = 0 ; k < that.secletgoods.length; k++){
                                that.secletgoods.eq(k).removeClass("active");
                                that.secletgoods.eq(k).attr("state",0);
                            }
                            that.state = 0 ;
                        };
                        that.calculationMoney();
                    });
               }else{
                   this.secletgoods.eq(i).on("click",function(){
                        if(that.secletgoods.eq(i).attr("state")==0){
                            that.secletgoods.eq(i).addClass("active");
                            that.secletgoods.eq(i).attr("state",1);
                        }else{
                            that.secletgoods.eq(i).removeClass("active");
                            that.secletgoods.eq(i).attr("state",0);
                        }
                        that.check();
                        that.calculationMoney();
                    });
               }
            }
        }
        check(){
            var that =this;
            // 判断单个物品是不是全部等于0/1，控制全选的状态；
            let stateAllGoods = 0;
            //  如果没选全选按钮但是商品都选了，则自动全选，如果全部商品没选全部清除。
            for(let i = 2 ; i < that.secletgoods.length-1; i++){
                stateAllGoods+=parseInt(that.secletgoods.eq(i).attr("state"));
            }
            if(stateAllGoods==that.secletgoods.length-3){
                // 全选
                that.secletgoods.eq(0).addClass("active");
                that.secletgoods.eq(1).addClass("active");
                that.secletgoods.eq(that.secletgoods.length-1).addClass("active");
                this.state = 1;
            }else{
                // 未全选
                that.secletgoods.eq(0).removeClass("active");
                that.secletgoods.eq(1).removeClass("active");
                that.secletgoods.eq(that.secletgoods.length-1).removeClass("active");
                this.state = 0;
            }
        }
        calculationMoney(){
            // 选购件数
            var num = [];
            // 选购总金额
            var money = [];
            // 获取选购的物品
            for(let i = 2 ; i < this.secletgoods.length-1; i++){
                if(this.secletgoods.eq(i).attr("state")==1){
                    // 获取单价
                    var m = this.secletgoods.eq(i).parent().parent().children(".content_goodsbox_price").children("span").text();
                    m = m.substr(1);
                    money.push(m);
                    // 获取数量
                    var n =this.secletgoods.eq(i).parent().parent().children(".content_goodsbox_num").children("div").children("div").eq(1).text();
                    num.push(n);
                }
            }
            // 共计件数
            var allNum = 0;
            // 共计金额
            var allMoney = 0;
            for(let i = 0 ; i < num.length ; i++){
                allNum += Number(num[i]);
                allMoney += Number(money[i])*Number(num[i]);
            }

            $("#goodsnum").text(`已选${allNum}件`);     
            $("#goodsmoney").text(`￥${allMoney}`);     
        
            if(allMoney>0){
                $("#settlement").css({
                    "background":"#845f3f",
                    "cursor":"pointer",
                });
            }else if(allMoney <=0){
                $("#settlement").css({
                    "background":"#e7e7e7",
                    "cursor":"no-drop",
                });
            }
        }
    }
    let calculation = new Calculation();

    // 负责修改商品数据
    class DataStorage{
        constructor(){}
        getData(data){
            this.data = data;
        }
        addEvent(){
            for(let i = 0 ; i < $(".reduceBtn").length ; i++){
                $(".reduceBtn").eq(i).on("click",()=>{
                    var num = Number($(".addBtn").eq(i).prev().text())-1;
                    if(num<=1){
                        num = 1;
                    }
                    $(".reduceBtn").eq(i).next().text(num);
                    this.data[i].num = num ;
                    this.calculationMoney();
                    calculation.calculationMoney();
                    this.setData(i,0);
                });
                $(".addBtn").eq(i).on("click",()=>{
                    var num = Number($(".addBtn").eq(i).prev().text())+1;
                    $(".addBtn").eq(i).prev().text(num);
                    this.data[i].num = num ;
                    this.calculationMoney();
                    calculation.calculationMoney();
                    this.setData(i,0);
                });

                $(".content_goodsbox_del").eq(i).on("click",()=>{
                    $(".content_goodsbox_del i").eq(i).parent().parent().css("display","none");
                    this.setData(i,1);
                });
            }

        }
        calculationMoney(){
            for(let i = 0 ; i <this.data.length ; i++){
                var price = Number(this.data[i].price);
                var num = Number(this.data[i].num);
                var m = price * num ;
                $(".content_goodsbox_allprice").eq(i).children("span").text("￥"+m);
            }
            dataStorage.setData();
        }
        setData(res,index){
            // res 传的是对应的索引 ,index 传的是修改还是删除，0是修改 1是删除
            if(index == 0 ){
                pAjax({
                    url:"http://localhost/XMYP/src/php/updata.php", 
                    data:{
                        user:this.data[res].user,
                        goodsid: this.data[res].goodsid,
                        num:this.data[res].num,
                    }
                }).catch((r)=>{
                    console.log(r);
                })
            }else if(index ==1){
                var goodsid = this.data[res].goodsid;
                pAjax({
                    url:"http://localhost/XMYP/src/php/delete.php", 
                    data:{
                        user:this.data[res].user,
                        goodsid: this.data[res].goodsid,
                    }
                }).catch((r)=>{
                    console.log(r);
                })
            }
            


        }

    }

    var dataStorage = new DataStorage();


    
}());
